<?php
namespace app\message\controller;
use think\Request;
use app\common\controller\Common;
class Message extends Common
{
	protected $model = null;
	protected $afterActionList = [
        'addOperLog'  =>  ['only'=>'edit,add,del'],
    ];

	public function _initialize()
	{
		parent::_initialize();
		$this->model = new \app\message\model\Message;
	}
	/**
	* 短信模版列表
	*/
	public function index()
	{
		if(Request()->isAjax()){
			$data = Request()->param('');

			return $this->getList($data);
		}
		if(Request()->isGet()){
			return $this->fetch('');
		}
	}
	/**
	* 短信详情
	*/
	public function details($ids = null)
	{
		if(Request()->isGet()){
			$row = $this->model->get($ids);
			$this->assign('row',$row);
			return $this->fetch('');
		}
	}
	/**
	* 短信编辑
	*/
	public function edit($ids = null)
	{
		if(Request()->isPost()){
			$data = Request()->param('');
			$this->log_id = $data['ids'];
			$data['message_id'] = $data['ids'];
			$res = $this->model->isUpdate(true)->allowField(true)->validate('Message.ajax')->save($data);
			if($res === false){
				$this->error($this->model->getError());
			}
			$this->success(config('status.SUCCESS_ONE'));
		}
		if(Request()->isGet()){
			$row = $this->model->get($ids);
			$this->assign('row',$row);
			return $this->fetch('');
		}
	}
	/**
	* 短信添加
	*/
	public function add($type = null)
	{
		if(Request()->isGet()){
			return $this->fetch('');
		}
		if(Request()->isPost()){
			$data = Request()->param('');
			$res = $this->model->isUpdate(false)->allowField(true)->validate('Message.ajax')->save($data);
			if($res === false){
				$this->error($this->model->getError());
			}
			$this->log_id = $this->model->message_id;
			$this->success(config('status.SUCCESS_ONE'));
		}
	}
	/**
	* 批量删除
	*/
	public function del()
	{
		if(Request()->isAjax()){
			$data = Request()->param('');
			$this->log_id = implode(',', $data['id']);
			try{
				$this->model->where('message_id','in',$data['id'])->delete();
				$this->success(config('status.SUCCESS_ONE'));
			}catch(Exception $e){
				$this->error(config('status.ERROR_FIVE'));
			}
			
		}
	}
	/******************************** 私有 ********************************/
	/**
	* 列表数据处理
	*/
	protected function handleList($data)
	{
		$where = [];
		if(isset($data['type']) && $data['type'] != ''){
			$where['type'] = $data['type'];
		}
		return $where;
	}
}